package com.nf.controller.home;

import com.google.gson.Gson;
import com.nf.controller.BaseServlet;
import com.nf.dao.SeatInfoDao;
import com.nf.dao.impl.SeatInfoDaoImpl;
import com.nf.util.GsonUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * 编写servlet ，实现根据放映ID、座位ID 来修改座位的状态
 */
@WebServlet("/api/UpdateSeatStatusByIdServlet.do")
public class UpdateSeatStatusByIdServlet extends BaseServlet {
    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BufferedReader reader= req.getReader();
        String str=null;
        String jsonParams="";
        while ((str=reader.readLine())!=null){
            jsonParams=jsonParams+str;
        }
        Map<String,String> map= GsonUtil.parseMap(jsonParams);

        int showId= Integer.parseInt(map.get("showId"));
        int status= Integer.parseInt(map.get("status"));
        int seatId= Integer.parseInt(map.get("seatId"));

//        String seatId= map.get("seatId");
//        String[] arr=seatId.split(" ");
//        SeatInfoDao dao=new SeatInfoDaoImpl();
//        int r=0;
//        for(int i=0;i< arr.length;i++){
//            int id= Integer.parseInt(arr[i+1]);
//
//            dao.updadeStatus(showId,id,status);
//            r++;
//        }


        SeatInfoDao dao=new SeatInfoDaoImpl();
        int r= dao.updadeStatus(showId,seatId,status);

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();

        if(r==1){
            out.print(new Gson().toJson(successJson("修改成功")));
        }else {
            out.print(new Gson().toJson(errorJson("修改失败")));
        }

        out.flush();
        out.close();


    }

//    @Override
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        int showId= Integer.parseInt(req.getParameter("showId"));
//        int status= Integer.parseInt(req.getParameter("status"));
//        String[] arr= req.getParameterValues("seatId");
//        System.out.println(arr);
//        System.out.println(new Gson().toJson(arr));
//        System.out.println("气死了");
//
//
//        int r=0;
//        for (String seatId : arr) {
//            System.out.println(seatId);
//            SeatInfoDao dao=new SeatInfoDaoImpl();
//            dao.updadeStatus(showId, Integer.parseInt(seatId),status);
//            r++;
//        }
//
//
//        resp.setContentType("application/json;charset=utf-8");
//        PrintWriter out = resp.getWriter();
//
//        if(r==arr.length){
//            out.print(new Gson().toJson(successJson("修改成功")));
//        }else {
//            out.print(new Gson().toJson(errorJson("修改失败")));
//        }
//
//        out.flush();
//        out.close();
//    }
}
